package com.xqboss.apps.service.consumer;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.consumer.ConsumerInviteRecord;
import com.xqboss.apps.domain.consumer.ConsumerRealName;
import com.xqboss.apps.dto.punch.GetWebPunchRecordDto;
import com.xqboss.apps.mapper.consumer.ConsumerInviteRecordMapper;
import com.xqboss.apps.vo.consumer.ConsumerInviteRecordVo;
import com.xqboss.apps.vo.sys.SysUserVo;
import com.xqboss.common.core.domain.entity.SysUser;
import com.xqboss.common.core.domain.model.RegisterBody;
import com.xqboss.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 邀请记录接口
 *
 * @author wangcong
 * @date 2023-09-06
 */
@Service
public class ConsumerInviteRecordService extends ServiceImpl<ConsumerInviteRecordMapper, ConsumerInviteRecord> {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ConsumerInviteRecordMapper consumerInviteRecordMapper;

    public void addInvite(RegisterBody user) {
        if (StrUtil.isBlank(user.getInvitationCode())) {
            return;
        }
        SysUser sys = sysUserMapper.selectUserByUserName(user.getUsername());
        if (ObjUtil.isNull(sys)) {
            return;
        }
        SysUser inviteUser = sysUserMapper.selectUserByInvitationCode(user.getInvitationCode());
        if (ObjUtil.isNotNull(inviteUser)) {
            ConsumerInviteRecord record = new ConsumerInviteRecord();
            record.setUserId(sys.getUserId());
            record.setNickName(sys.getNickName());
            record.setInvitId(inviteUser.getUserId());
            record.setPhone(sys.getUserName());
            save(record);
        }
    }

    public List<ConsumerInviteRecordVo> inviteRecord(Long userId) {
        return  consumerInviteRecordMapper.inviteRecord(userId);
    }

    public List<ConsumerInviteRecordVo> webRecordList(GetWebPunchRecordDto dto) {
        return  consumerInviteRecordMapper.webRecordList(dto);
    }


    /**
     * 邀请用户数
     * @param userId
     * @return
     */
    public long getInviteNum(Long userId) {
        return this.lambdaQuery().eq(ConsumerInviteRecord::getInvitId,userId).count();
    }
}
